package com.ericsson.enap.modules.ebmLog.task;

import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.enap.modules.ebmLog.entity.CcSccVO;
import com.ericsson.enap.modules.ebmLog.entity.EbmEventVO;
import org.slf4j.Logger;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @description: ebm多线程统计入库任务
 * @author: wangjingshi
 * @create: 2021-04-20 15:34
 */
public class EbmStatisticAndLoadTask implements Runnable{

    private Logger log = LoggerUtils.Logger(LogFileName.EbmLogJob);
    private String netid;
    private String localPath;
    private CountDownLatch latch;
    private String dateTime;
    private Map<String,EbmEventVO> eventMap;
    private MongoTemplate mongoTemplate;

    public EbmStatisticAndLoadTask(String netid, String localPath, CountDownLatch latch, String dateTime, Map<String, EbmEventVO> eventMap, MongoTemplate mongoTemplate) {
        this.netid = netid;
        this.localPath = localPath;
        this.latch = latch;
        this.dateTime = dateTime;
        this.eventMap = eventMap;
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public void run() {
        CountDownLatch inner_latch = new CountDownLatch(eventMap.size()+1);
        //一、统计cc和scc成功失败数
        this.executeCCAndSccWork(inner_latch);
        //二、按照原来ebm的模式统计失败的event，最后录库statistics表
        this.executeFailedEbm(eventMap,inner_latch);
        try {
            inner_latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            latch.countDown();
        }
    }

    private void executeFailedEbm(Map<String, EbmEventVO> eventMap, CountDownLatch inner_latch) {
        for(Map.Entry<String, EbmEventVO> entry : eventMap.entrySet()) {
            EbmAnalysisAndInsertTask task = new EbmAnalysisAndInsertTask(localPath,dateTime,netid,entry.getKey(),entry.getValue().getEventName(),inner_latch);
            new Thread(task).start();
        }
    }

    private void executeCCAndSccWork(CountDownLatch inner_latch) {
        EbmDoCcSccTask ccSccTask = new EbmDoCcSccTask(netid,localPath,dateTime,eventMap,inner_latch,mongoTemplate);
        new Thread(ccSccTask).start();
    }

}
